using System;
using System.Text;
using System.Xml.Serialization;
using System.Collections.Generic;

using usm.SolDev.Common;
using usm.SolDev.Common.Constants;

namespace usm.SolDev.DataBase.DataObjects
{
    /// <summary>
    ///     Store all associated user data. 
    /// </summary>
    [Serializable]
    public class UserInfoData : DataObject
    {

        #region Private properties
        private int id;
        private string login;
        private string password;
        private string name;
        private int group;
        private List<Permissions> userPermitions = new List<Permissions>();
        #endregion

        #region C'tor
        public UserInfoData () {}

        public UserInfoData (int id, string login, string password, string name, int group )
        {
            this.id = id;
            this.login = login;
            this.password = password;
            this.name = name;
            this.group = group;
        }
        #endregion

        #region Public properties
        /// <summary>
        ///     Stores or returns user login
        /// </summary>
        [XmlAttributeAttribute(DataType = "string")]
        public string Login
        {
            get { return login; }

            set 
            {
                if (value == null || value.Trim().Length == 0 )
                {
                    throw new SolDevException(SolDevExceptionKey.UserLoginCannotBeEmpty);
                }

                login = value;
            }
        }

        /// <summary>
        ///     Hold's the user's password (encrypted). The assignment operation,
        /// encrypt's immediately the password using SHA256 alhorithm.
        /// </summary>
        [XmlIgnore()]
        public string Password
        {
            get { return password; }
            set 
            {
                password = value;
            }
        }

        /// <summary>
        ///     Encrypt/decrypt password
        /// </summary>
        [XmlAttributeAttribute(DataType = "string")]
        public string dbPassword
        {
            get
            {
                return usm.SolDev.Common.Cryptography.CryptRSA(password);
            }

            set
            {
                password = usm.SolDev.Common.Cryptography.DecryptRSA(value);
            }
        }

        /// <summary>
        ///     Hold's and sets the users name (no restriction)
        /// </summary>
        [XmlAttributeAttribute(DataType = "string")]
        public string Name
        {
            get { return name; }
            set { name = value;}
        }

        /// <summary>
        ///     Hold's user group, optional. If it is zero, the user is not
        /// grouped at all.
        /// </summary>
        [XmlAttributeAttribute(DataType = "integer")]
        public int Group
        {
            get { return group; }
            set { group = value; }
        }

        /// <summary>
        ///     User associated ID
        /// </summary>
        [XmlAttributeAttribute(DataType = "integer")]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        ///     Associated user permissions
        /// </summary>
        [XmlIgnore]
        public List<Permissions> UserPermitions
        {
            get { return userPermitions; }
            set { userPermitions = value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public bool HasPermission(Permissions.UserPermission permission)
        {
            foreach (Permissions perm in userPermitions)
            {
                if (perm.Id == permission)
                {
                    return true;
                }
            }

            return false;
        }


    }
}
